require(['init'], function () {
  var tid = location.search.urlToObj().tid;
  var endtime = location.search.urlToObj().endtime;
  var begintime = location.search.urlToObj().begintime;
  var teachername = location.search.urlToObj().teachername;
  $('#teachername').html(teachername);
  ajaxMethod({
     url:'/api/statistic/teacher/'+tid,
     datatype: 'json',
     type: 'get',
     data: {
         "begintime": begintime,
         "endtime":endtime,

     },
     success:function(data){
       var $studentsClass=$('.studentsClass').html('');
       var $thead=$('<thead>\
                           <tr class="enroll" >\
                             <th >'+i18next.t('table.no')+'</th>\
                             <th >'+i18next.t('table.lessontype')+'</th>\
                             <th >'+i18next.t('table.coursename')+'</th>\
                             <th >'+i18next.t('table.coursedates')+'</th>\
                             <th >'+i18next.t('table.schooltime')+'</th>\
                             <th >'+i18next.t('table.duration')+'</th>\
                             <th >'+i18next.t('table.classhours')+'</th>\
                             <th style="background:rgba(193, 234, 197, 0.3);color:#080202;">'+i18next.t('table.firstclassroomtime')+'</th>\
                             <th style="background:rgba(193, 234, 197, 0.3);color:#080202;">'+i18next.t('table.lastclassroomtime')+'</th>\
                             <th style="background:rgba(193, 234, 197, 0.3);color:#080202;">'+i18next.t('table.duration')+'</th>\
                             <th >'+i18next.t('info.studentlist')+'</th>\
                           </tr>\
                       </thead>').appendTo($studentsClass);
         var $tbody=$('<tbody></tbody>').appendTo($studentsClass);
         function getDurationMinute(begintime, endtime) {
             if (begintime != null || endtime != null) {
                 var begintime = +new Date(begintime.replace(' ', ' '));
                 var endtime = +new Date(endtime.replace(' ', ' '));
                 return (endtime - begintime) / (60 * 1000) + i18next.t('table.minute');
             } else {
                 return '--';
             }
         }
         $.each(data,function(i,t){
          var students=t.students;
           var btimel="",btime="";
           if(t.begintime!="null"){
               btimel = t.begintime.substr(0,10);
               btime = t.begintime.substr(11,5);
           }
           var a=JSON.stringify(t.begintime);
           var day=(a).replace(/\-/g,"");
           //console.log(day)
           $tr=$('<tr data-id='+day+' class="enroll"></tr>').appendTo($tbody);
               $tr.append('<td>1</td>');
               $tr.append('<td>'+t.coursetypename+'</td>');
               $tr.append('<td>'+t.name+'</td>');
               $tr.append('<td>'+btimel+'</td>');
               $tr.append('<td>'+btime+'</td>');
               $tr.append('<td>'+getDurationMinute(t.begintime, t.endtime)+'</td>');
               $tr.append('<td>'+t.scheduletimes+'</td>');
               $tr.append('<td style="background:rgba(193, 234, 197, 0.3);color:#080202;">'+(t.firstEnterTime==null? "0000-00-00 00:00":t.firstEnterTime.substr(0,16))+'</td>');
               $tr.append('<td style="background:rgba(193, 234, 197, 0.3);color:#080202;">'+(t.lastExitTime==null?"0000-00-00 00:00":t.lastExitTime.substr(0,16))+'</td>');
               $tr.append('<td style="background:rgba(193, 234, 197, 0.3);color:#080202;">'+(t.studyTime==null?"0":Math.floor(t.studyTime/60))+''+i18next.t('table.minute')+'</td>');
               var $td=$('<td></td>').appendTo($tr);
               var $ul=$("<ul></ul>").appendTo($td);
               var $li=$("<li></li>").appendTo($ul);
               $.each(students,function(index,item){
                 $('<a>'+item.name+',</a>').appendTo($li)
               })
               big();
         })
         var len = $("#tableSt").find('table tr').length;
         for (var i = 1; i < len; i++) {
           $("#tableSt").find('table tr:eq(' + i + ') td:first').text(i);
         }
       }
    })
  function big() {
      var tr = $(".studentsClass").find("tr");
      var initList = [], domArr = [];

      $.each(tr, function (a, k) {
          initList[a] = $(this).attr('data-id');
          //console.log(initList[a]);

      })
      /* 从大到小排序*/
      function bigToSmall() {
          getDom();
          $.each(domArr, function (i) {
              $.each(domArr, function (j) {
                  if (domArr[i].attr('data-id') < domArr[j].attr('data-id')) {
                      mid = domArr[j]; domArr[j] = domArr[i]; domArr[i] = mid;
                  }
              })
          })
          appendDom()
      }
      /*取出所有tr放进数组*/
      function getDom() {
          $(tr).each(function (m) {
              domArr[m] = $(this);
          })
      }
      /*将数组中的tr插入tbody(原ul中的li自动删除。DOM元素的特性。) */
      function appendDom() {
          $.each(domArr, function (d) {
               $(".studentsClass ").append(domArr[d]);
          })
      }
      bigToSmall();
    }


})
